#include "parafluidatom.h"
#include "parafluidemitter.h"
#include "parafluidvector.h"

#include <cstdlib>
#include <ctime>

using namespace ParaFluid;

Emitter::~Emitter()
{
	/* does nothing */
}

Emitter::Emitter()
	: zone(Vector(0.0, 0.0, 0.0), Orientation(0.0, 0.0, 0.0), Vector(1.0, 1.0, 1.0))
	, substance()
	, _rate(1)
	, _remaining(0)
{
	/* does nothing */
}

int Emitter::rate() const
{
	return _rate;
}

void Emitter::setRate(int value)
{
	if(value > 0)
	{
		_rate = value;
	}
}

int Emitter::remaining() const
{
	return _remaining;
}

void Emitter::setRemaining(int value)
{
	if(value >= 0)
	{
		_remaining = value;
	}
}

void Emitter::emitAtoms(SPH & sph)
{
	// Initialize Random Seed
	static bool needSeed = true;
	if(needSeed)
	{
		needSeed = false;
		std::srand(std::time(0));
	}
	
	// Number of Atoms to Emit
	int count = _rate;
	if(count > _remaining)
	{
		count = _remaining;
	}
	_remaining -= count;
	
	// Insert Random Atoms
	while(count > 0)
	{
		Vector random;
		random.x = zone.position.x + zone.extent.x * float(rand()) / float(RAND_MAX);
		random.y = zone.position.y + zone.extent.y * float(rand()) / float(RAND_MAX);
		random.z = zone.position.z + zone.extent.z * float(rand()) / float(RAND_MAX);
		sph.atomVoxelizer.insert(Atom(random, substance));
		count--;
	}
}
